﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Model;
using Presenters.WPF;
using Presenters.WPF.Views;

namespace AirlinePilotAcademy_WPF.UI_Administrator.Students
{
    /// <summary>
    /// Interaction logic for NewStudent.xaml
    /// </summary>
    public partial class NewStudent : Window, INewStudentView
    {
        NewStudentPresenter presenter;

        Student studentModify = new Student() { id = -1};
        Student studentView = new Student() { id = -1};
        public NewStudent()
        {
            InitializeComponent();
            presenter = new NewStudentPresenter(this);
            this.ModifyB.Visibility = Visibility.Hidden;
        }
        //MODIFY STUDENT
        public NewStudent(Student studentModify)
        {
            InitializeComponent();
            presenter = new NewStudentPresenter(this);
            this.titleL.Content = "Modificar Estudiante";
            this.SaveB.Visibility = Visibility.Hidden;
            this.studentModify = studentModify;
            this.Title = "Modificar Estudiante";
            FillAllTextBoxesM();
            DisableNonEditCampusM();
        }

        private void DisableNonEditCampusM()
        {
            //considering teacher opinion, all TB should be able to modify except the ID
            
            /*this.lastnameTB.IsEnabled = false;
            this.mlastnameTB.IsEnabled = false;
            this.nameTB.IsEnabled = false;
            this.sexCB.IsEnabled = false;
            this.birthdayDP.IsEnabled = false;
            this.birthplaceTB.IsEnabled = false;
            this.fNameTB.IsEnabled = false;
            this.mNameTB.IsEnabled = false;*/
            this.idTB.IsEnabled = false;

        }
        private void FillAllTextBoxesM()
        {
            this.lastnameTB.Text = studentModify.lastname.ToString();
            this.mlastnameTB.Text = studentModify.mlastname.ToString();
            this.nameTB.Text = studentModify.name.ToString();
            this.sexCB.Text = studentModify.sex.ToString();
            this.birthdayDP.SelectedDate = studentModify.birthday.Date;
            this.birthplaceTB.Text = studentModify.birthplace.ToString();
            this.idTB.Text = studentModify.id.ToString();
            this.issuedInTB.Text = studentModify.issuedIn.ToString();
            this.professionTB.Text = studentModify.profession.ToString();
            this.liceAeroTB.Text = studentModify.liceAero.ToString();
            this.addressTB.Text = studentModify.address.ToString();
            this.zoneTB.Text = studentModify.zone.ToString();
            this.telephoneTB.Text = studentModify.telephone.ToString();
            this.emailTB.Text = studentModify.email.ToString();
            this.fNameTB.Text = studentModify.fName.ToString();
            this.fProfessionTB.Text = studentModify.fProfession.ToString();
            this.mNameTB.Text = studentModify.mName.ToString();
            this.mProfessionTB.Text = studentModify.mProfession.ToString();
            this.observationsTB.Text = studentModify.observations.ToString();
        }
        //****END MODIFY STUDENT
        //VIEW STUDENT
        public NewStudent(Student viewStudent, string isView)
        {
            InitializeComponent();
            presenter = new NewStudentPresenter(this);
            this.titleL.Content = "Ver Estudiante";
            this.SaveB.Visibility = Visibility.Hidden;
            this.ModifyB.Visibility = Visibility.Hidden;
            this.CleanB.Visibility = Visibility.Hidden;
            this.CancelB.Content = "Cerrar";
            this.studentView = viewStudent;
            this.Title = "Ver Estudiante";
            FillAllTextBoxesV();
            DisableAllCampusV();
        }

        private void DisableAllCampusV()
        {
            this.lastnameTB.IsEnabled = false;
            this.mlastnameTB.IsEnabled = false;
            this.nameTB.IsEnabled = false;
            this.sexCB.IsEnabled = false;
            this.birthdayDP.IsEnabled = false;
            this.birthplaceTB.IsEnabled = false;
            this.idTB.IsEnabled = false;
            this.issuedInTB.IsEnabled = false;
            this.professionTB.IsEnabled = false;
            this.liceAeroTB.IsEnabled = false;
            this.addressTB.IsEnabled = false;
            this.zoneTB.IsEnabled = false;
            this.telephoneTB.IsEnabled = false;
            this.emailTB.IsEnabled = false;
            this.fNameTB.IsEnabled = false;
            this.fProfessionTB.IsEnabled = false;
            this.mNameTB.IsEnabled = false;
            this.mProfessionTB.IsEnabled = false;
            this.observationsTB.IsEnabled = false;
        }

        private void FillAllTextBoxesV()
        {
            this.lastnameTB.Text = studentView.lastname.ToString();
            this.mlastnameTB.Text = studentView.mlastname.ToString();
            this.nameTB.Text = studentView.name.ToString();
            this.sexCB.Text = studentView.sex.ToString();
            this.birthdayDP.SelectedDate = studentView.birthday.Date;
            this.birthplaceTB.Text = studentView.birthplace.ToString();
            this.idTB.Text = studentView.id.ToString();
            this.issuedInTB.Text = studentView.issuedIn.ToString();
            this.professionTB.Text = studentView.profession.ToString();
            this.liceAeroTB.Text = studentView.liceAero.ToString();
            this.addressTB.Text = studentView.address.ToString();
            this.zoneTB.Text = studentView.zone.ToString();
            this.telephoneTB.Text = studentView.telephone.ToString();
            this.emailTB.Text = studentView.email.ToString();
            this.fNameTB.Text = studentView.fName.ToString();
            this.fProfessionTB.Text = studentView.fProfession.ToString();
            this.mNameTB.Text = studentView.mName.ToString();
            this.mProfessionTB.Text = studentView.mProfession.ToString();
            this.observationsTB.Text = studentView.observations.ToString();
        }
        //****END VIEW STUDENT
        public string lastname
        {
            get { return lastnameTB.Text; }
        }
        public string mlastname
        {
            get { return mlastnameTB.Text; }
        }
        public string name
        {
            get { return nameTB.Text; }
        }
        public string sex
        {
            get { return sexCB.Text; }
        }
        public DateTime birthday
        {
            get { return birthdayDP.SelectedDate.Value; }
        }
        public string birthplace
        {
            get { return birthplaceTB.Text; }
        }
        public int id
        {
            get { return Convert.ToInt32(idTB.Text); }
        }
        public string issuedIn
        {
            get { return issuedInTB.Text; }
        }
        public string profession
        {
            get { return professionTB.Text; }
        }
        public string liceAero
        {
            get { return liceAeroTB.Text; }
        }
        public string address
        {
            get { return addressTB.Text; }
        }
        public string zone
        {
            get { return zoneTB.Text; }
        }
        public string telephone
        {
            get { return telephoneTB.Text; }
        }
        public string email
        {
            get { return emailTB.Text; }
        }
        public string fName
        {
            get { return fNameTB.Text; }
        }
        public string fProfession
        {
            get { return fProfessionTB.Text; }
        }
        public string mName
        {
            get { return mNameTB.Text; }
        }
        public string mProfession
        {
            get { return mProfessionTB.Text; }
        }
        public string observations
        {
            get { return observationsTB.Text; }
        }
        public string message
        {
            set { messageL.Content = value; }
        }
        private void SaveB_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                presenter.SubmitNewStudent();
                this.Close();
            }
            catch (Exception ex)
            {
                messageL.Content = ex.Message;
            }
        }

        private void CancelB_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        private void TextBox_idValidation(object sender, TextChangedEventArgs e)
        {
            if (!String.IsNullOrEmpty(this.idTB.Text))
            {
                if (presenter.ValidateIDText() == false)
                {
                    MessageBox.Show("Este campo solo admite numeros!","Info",MessageBoxButton.OK, MessageBoxImage.Information);
                    this.idTB.Text = "";
                    messageL.Content = "";
                }
            }
        }
        private void lastnameTB_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            foreach (char ch in e.Text)
            {
                if (!Char.IsLetter(ch))
                {
                    e.Handled = true;
                }
            }
        }
        private void onlyDigits_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            foreach (char ch in e.Text)
            {
                if (!Char.IsDigit(ch))
                {
                    e.Handled = true;
                }
            }
        }

        private void ModifyB_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                presenter.SubmitModifyStudent();
                this.Close();
            }
            catch (Exception ex)
            {
                messageL.Content = ex.Message;
            }
        }

        private void CleanB_Click(object sender, RoutedEventArgs e)
        {
            if(this.ModifyB.Visibility == Visibility.Hidden)
                EraseTB();
            EraseTBExcludingID();

        }
        private void EraseTB()
        {
            idTB.Text= "";
            nameTB.Text = "";
            lastnameTB.Text = "";
            mlastnameTB.Text = "";
            sexCB.Text = "";
            birthdayDP.SelectedDate = null;
            birthplaceTB.Text = "";
            liceAeroTB.Text = "";
            professionTB.Text = "";
            addressTB.Text = "";
            zoneTB.Text = "";
            telephoneTB.Text = "";
            fNameTB.Text = "";
            fProfessionTB.Text = "";
            mNameTB.Text = "";
            mProfessionTB.Text = "";
            observationsTB.Text = "";
            issuedInTB.Text = "";
            emailTB.Text = "";
        }
        private void EraseTBExcludingID() //for modifying
        {
            nameTB.Text = "";
            lastnameTB.Text = "";
            mlastnameTB.Text = "";
            sexCB.Text = "";
            birthdayDP.SelectedDate = null;
            birthplaceTB.Text = "";
            liceAeroTB.Text = "";
            professionTB.Text = "";
            addressTB.Text = "";
            zoneTB.Text = "";
            telephoneTB.Text = "";
            fNameTB.Text = "";
            fProfessionTB.Text = "";
            mNameTB.Text = "";
            mProfessionTB.Text = "";
            observationsTB.Text = "";
            issuedInTB.Text = "";
            emailTB.Text = "";
        }
    }
}
